#!/bin/sh
#
# mindist: create minimum distribution for running cputil
#
# Copyright (C) 2016,2021 Matthew R. Wette
# 
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

prefix=@prefix@
libexecdir=@libeexecdir@
libdir=@libdir@
includedir=@includedir@

fail () {
  echo "usage: mindist DESTDIR=<dir>"
  echo "example:"
  echo "       mindist DESTDIR=/var/tmp/bindist"
  exit 1
}

if [ $# -ne 1 ]; then fail; fi
case $1 in 
DESTDIR=*) destdir=`echo $1 | sed -e 's/DESTDIR=//'` ;;
*) fail ;;
esac
if echo $destdir | grep '.*\/$'; then
    true
else
    destdir=$destdir/
fi

cu_execdir=`echo $libexecdir | sed -e 's/valgrind/cputil/'`
top=$destdir$prefix
exe=$destdir$libexecdir
lib=$destdir$libdir
inc=$destdir$includedir
cu_exe=$destdir$cu_execdir

if [ ! -f $top/bin/valgrind ]; then
   echo "do not see $top/bin/valgrind -- quitting"
   exit 1
fi
     
mv $top/bin/valgrind $exe
rm $top/bin/*

if [ -f $top/include/valgrind/libvex_ir.h ]; then
  cp $top/include/valgrind/libvex_ir.h $exe
fi
if [ -f $top/include/valgrind/cputil.h ]; then
   cp $top/include/valgrind/cputil.h $inc
   cp $top/include/valgrind/cputil.h $exe
fi
rm -rf $top/include/valgrind

rm -rf $top/share
rm -rf $top/lib/pkgconfig

rm -f $exe/vgpreload_dhat*
rm -f $exe/vgpreload_drd*
rm -f $exe/vgpreload_exp*
rm -f $exe/vgpreload_helgrind*
rm -f $exe/vgpreload_massif*
rm -f $exe/vgpreload_memcheck*

rm -f $exe/s390*
rm -f $exe/power*
rm -f $exe/mips*
rm -f $exe/arm*

rm -f $exe/none*
rm -f $exe/memcheck*
rm -f $exe/massif*
rm -f $exe/getoff*
rm -f $exe/helgrind*
rm -f $exe/lackey*
rm -f $exe/exp*
rm -f $exe/drd*
rm -f $exe/cachegrind*
rm -f $exe/callgrind*

rm -f $lib/*.a
rm -f $lib/libmpiwrap*.so
rm -f $exe/dh*
rm -f $exe/*.xml

if [ ! -d $cu_exe ]; then mv $exe $cu_exe; fi

# cleanup
rm -rf $exe $lib
if [ -d $destdir$prefix/libexec ]; then rmdir $destdir$prefix/lib; fi

# generate driver script
echo "#!/bin/sh"                                       >$top/bin/cputil
echo "VALGRIND_LIB=$cu_execdir"                        >>$top/bin/cputil
echo "VALGRIND_LAUNCHER=$cu_execdir"                   >>$top/bin/cputil
echo "export VALGRIND_LIB VALGRIND_LAUNCHER"           >>$top/bin/cputil
echo '$VALGRIND_LIB/valgrind -q --tool=cputil "$@"'    >>$top/bin/cputil
chmod +x $top/bin/cputil

# --- last line ---
